package com.aita.diamond.service

import com.gd.magic.Service
import com.aita.diamond.Diamond
import com.gd.domain.party.Organization
import com.aita.diamond.PurchaseBill
import com.aita.diamond.Order

class DiamondService implements Service {
			
	public static final String FORMAT_YEARMONTH = "yyyyMM"
	
	def deleteDiamond(long id){
		logger.debug("id  ...............")
		def now = new Date();
		Diamond diamond = Diamond.load(id)
		assert diamond : X("未找到"+ id)
		diamond.remove()
	}
	
	
	//新增 及 修改 钻石信息
	def saveDiamond(Diamond diamond, String orgName,long purchaseBillId) {
		Organization org = Organization.loadByName(orgName)
		assert diamond: X("钻石为空")
		
		diamond.diamondType = 1
		diamond.diamondStatus = 1
		
		if(purchaseBillId){
			def purchaseBill=PurchaseBill.load(purchaseBillId)
			diamond.purchaseBill=purchaseBill
		}
		
		logService.log(diamond.id == null ?101022:101024 ,(diamond.id == null ?"新增":"修改") +"钻石信息(" + diamond.certificateNo +")")
		diamond.org = org
		diamond.persistOrMerge()
	}
	
	//修改钻石库存位置 
	def saveDiamondOrg(Diamond diamond, String orgName,long purchaseBillId) {
		Organization org = Organization.loadByName(orgName)
		assert diamond: X("钻石为空")
		
		diamond.diamondType = 1
		diamond.diamondStatus = 1
		
		if(purchaseBillId){
			def purchaseBill=PurchaseBill.load(purchaseBillId)
			diamond.purchaseBill=purchaseBill
		}
		diamond.org = org
		
		logService.log(101020,"修改库存位置为"+orgName)
		//TODO save log
		diamond.persistOrMerge()
	}
	
	
	//添加 更新 订购钻石
	def saveDiamondForOrder(Diamond diamond, Organization org, Order order) {
		assert diamond: X("钻石为空")
		diamond.order = order
		diamond.persistOrMerge()
	}
	
	def loadDiamond(long id){
		logger.debug( "  id...............") 
		def now = new Date();
		Diamond diamond = Diamond.load(id)
		assert diamond : X("未找到"+ id)
		
		//导出 增加 价格 修改 权限
		//def exportFlag = false
		//def oper =  loginService.currentOperator()
		//def gm = GroupMemeber.load(oper)
		//def g = Group.load(gm.group)
		
		//if(g.name == '采购经理')
		//{
		//	exportFlag = true
		//}
		Organization org = diamond.org
		def result = [diamond:diamond,org:org]
		return result;
	}
}